$(document).ready(function(){
	
	$("#addGoodsFormDiv").dialog({
		autoOpen: false,
		modal: true,
		close:function(){
			$("input[type='text']").val("");
			$("#goodsID").val("-1");
		}
	});
	
	$("#addButton").click(function(){
		$("#addGoodsFormDiv").dialog("open");
	});
	
	$("#saveUpdateButton").click(function() {
		
		if($("#name").val()=="" || $("#price").val()==""){
			alert("所有项不能为空");
			return;
		}
		$("#saveUpdateButton").attr("disabled","disabled");
		var uri;
		if($("#goodsID").val()==-1){
			uri = "/MainMenu/configuration/insertGoods.htm";
		}else{
			uri = "/MainMenu/configuration/updateGoods.htm";
		}
		
		$.post(getRootPath()+ uri,
			   $("#goodsForm").serializeArray(),
			   function(data) {
					if(data.isSuccessfull==true){
						alert("保存成功!");
					}else{
						alert(data.error);
					}
					$("#addGoodsFormDiv").dialog("close");
					$("#saveUpdateButton").removeAttr("disabled");
					$("#goodsTable").load(getRootPath()+ "/MainMenu/configuration/goodsList.htm",{pageIndex:1});
				});
	});
	
	$("#goodsTable").load(getRootPath()+ "/MainMenu/configuration/goodsList.htm",{pageIndex:1});
});

function edit(id){
	$("#goodsForm").dialog("open");
}
